package eval.deploy;

import cern.jet.random.AbstractDistribution;
import eval.utilities.configuration.ExperimentConfiguration;
import eval.utilities.random.ProbabilisticDistributionsEnum;
import eval.vm.VMConfiguration;

/**
 * This class assign for each vm the CPU and MEMORY demand in each tick throughout the experiment. 
 * the CPU units are set according to  the random/constant distribution at a specific tick.
 * the memory units are set according to the average between the minimum and the maximum 
 * memory specified  at the configuration.
 * 
 *  @author Amit Peled   <amit.kapina@gmail.com>
 * @author Igal Kaufman <igal.kaufman@gmail.com>
 * */
public class VMRealDemand {

	private static ExperimentConfiguration conf = ExperimentConfiguration.getSingleton();

	private int[] cpuDemand;
	private int memDemand;
	private int ticklifeStart;

	/**
	 * @Constructor.
	 * @param CPUdemandDistribution the distribution of the VM
	 * @param MemDemand the fix MEM of VM
	 * @param vmConfiguration the VM configuration 
	 * @param tick the first tick that VM appear 
	 * @param lifeTime VM life time
	 */
	public VMRealDemand(AbstractDistribution CPUdemandDistribution,
			int MemDemand,VMConfiguration vmConfiguration, int tick, int lifeTime) {
		ticklifeStart = tick;
		this.cpuDemand = new int[lifeTime+1];
		this.memDemand =0;
		initHistory(CPUdemandDistribution,MemDemand,lifeTime,tick);
	}

	/**
	 * Get the CPU demand at tick
	 * @param tick
	 * @return the CPU demand
	 */
	public int getCPUDemandAtTick(int tick) {
		return cpuDemand[tick-ticklifeStart];
	}

	/**
	 *  Get the MEM demand at tick
	 * @param tick
	 * @return the MEM demandS
	 */
	public int getMEMDemandAtTick(int tick) {
		return memDemand;
	}

	/*
	 * Initial the demand of VM to is life time
	 * @param demandDistribution
	 * @param MEMDemand
	 * @param lifeTime
	 * @param tick
	 */
	private void initHistory(AbstractDistribution demandDistribution,int MEMDemand, int lifeTime,
			int tick) {
		for (int i = 0; i < lifeTime; i++) {
			if(i<conf.experimentTicks) { 
				switch (ProbabilisticDistributionsEnum
						.valueOf(conf.demandDistribution)) {
				//case CONSTANT similar to other cases.(should be deleted)
						case CONSTANT:
							cpuDemand[i] = demandDistribution.nextInt();
							//MEM are fix
							memDemand = conf.memDemandDistributionVal;
							break;
						default:
							cpuDemand[i] = demandDistribution.nextInt();
							memDemand = MEMDemand;
							break;
				}
			}
		}
	}
}
